#!/usr/bin/expect -f

set env(JAVA_HOME) {{es_basedir}}/jdk
set env(PATH) "$env(JAVA_HOME)/bin:$env(PATH)"

# 设置超时时间（-1 表示永不超时）
set timeout -1

# 设置ES安装目录（请根据实际情况修改）
set es_home "{{ es_basedir }}"

# 设置要为所有内置用户设置的密码（请修改为你的强密码）
set password "{{ es_xpack_pwd }}"

# 启动 elasticsearch-setup-passwords 交互式命令
spawn $es_home/bin/elasticsearch-setup-passwords interactive

# 等待初始提示并确认继续
expect "Please confirm that you would like to continue*"
send "y\r"

# 为 elastic 用户设置密码
expect "Enter password for *elastic*:"
send "$password\r"
expect "Reenter password for *elastic*:"
send "$password\r"

# 为 apm_system 用户设置密码
expect "Enter password for *apm_system*:"
send "$password\r"
expect "Reenter password for *apm_system*:"
send "$password\r"

# 为 kibana_system 用户设置密码
expect "Enter password for *kibana_system*:"
send "$password\r"
expect "Reenter password for *kibana_system*:"
send "$password\r"

# 为 logstash_system 用户设置密码
expect "Enter password for *logstash_system*:"
send "$password\r"
expect "Reenter password for *logstash_system*:"
send "$password\r"

# 为 beats_system 用户设置密码
expect "Enter password for *beats_system*:"
send "$password\r"
expect "Reenter password for *beats_system*:"
send "$password\r"

# 为 remote_monitoring_user 用户设置密码
expect "Enter password for *remote_monitoring_user*:"
send "$password\r"
expect "Reenter password for *remote_monitoring_user*:"
send "$password\r"

# 等待命令完成
expect eof

